Variables

 

Introduction

Changes, changes & more changes ! There are more of these every day. Everything started with 'em and they'll be around to the very end.

Changes = Variables

When you go for shopping and spend all your money there - variable called "Wallet" is changed from state "Full" to "Empty".

Travelling from one country to another makes change to your variable location.

Eating ice-cream reduces it's quantity in the box.

Computers couldn't operate without streams of flip-flopping zeroes and ones.

It's hard to imagine how dull everything would be without variables. Especially computers. That's why computer software uses 'em a lot. Being multimedia authoring tool (intended for entertainment stuff as opposite to The Dull World) - variables could prove to be quite important for MMB, couldn't they ? Your application depends on variable data.

Data put into variables is meant to be directly changed by your program.


The glass on images above represents constant label (container).
Content in glass is variable. It's changing as time passes.

Variables in MMB are used for every segment of data your program can get or put.


There are two sorts of variables MMB uses:

  • Integer (number) variables - used for storing numbers, these variables are important for MMB's script math operations
  • String (text) variables - store all kinds of characters, words, sentences, paragraphs...


Variables consist of:

  • variable labels: names you'll refer to when using 'em. Variables are represented with certain labels, used as value descriptors and are composed of alphanumerical characters (for e.g. vase, date, name, address, email1, email2...)

  • variable contents: numbers for numerical variables, letters for string variables

 

Labels and content of variables are specific to variable type and are discussed below.

Notice for MMB users with experience in programming: MMB doesn't require "declaring" of variables prior to their use. It takes care of 'em automatically, so your only job is to use them.


Numerical Variables

Some tasks in scripting require math operations. To perform these, you need - numbers. That's where numerical variables take place. You'll use 'em as containers for numbers.

There are two kinds of numbers you'll store in numerical variables:

  •  real (floating point) numbers, like: 43.92, 1.2938, 441.1, 881824.2
  •  integer (round) numbers, for example: 43, 21, 1, 4932, 6884

 


If we take this crystal glass and put numbers in it - we get numerical variable !

Just like that glass on image above, important part of numerical variable in MMB is label.
Here are some examples of valid labels:

i
n
month
1st_one
click_count
NoOfWords
5thUser
glass


It is recommended to use meaningful & descriptive labels, not too short, not too long - just enough to describe it's contents. If you'll use numerical variable to store number of mouse clicks, label of that variable should be MouseClicks or NoOfMouseClicks.

The most important part of this story is - assigning of variable contents.


In MMB script code lines, it is being done by:

a) writing label name:

glass

b) followed by equal sign:

=

c) and writing content (value) of variable at the end, behind label and equal sign:

5415


All together, MMB code line for assigning of number to numerical variable looks like this:

glass=5415


There ! With this line you instructed MMB: "Take a glass and put number 5415 in it."

Where will you give that instruction ? In MMB's Script Editor, of course. That's where you'll write your very first MMB code line (if you didn't cheat and skipped all this boring stuff) ;)

Very nice. Now, is this all you can do ? Assign fixed numbers and never change 'em again ?

Nope !

It is important to have ability of assigning value of one variable to the value of another variable. In other words: if you draw one caricature and give it to your kid:

...what will you do if other kid wants one too ?

You will copy it - or draw it again !

face1
=
face2


In MMB script code lines, it is being done by:

a) writing label of destination numerical variable (one that accepts value):

face2

b) followed by equal sign:

=

c) and writing label of source numerical variable (one that gives value):

face1


All together, MMB code line for assigning of content of one numerical variable to another numerical variable looks like this::

face2=face1

If variable face1 contains number 328, code line above will assign the same value to face2 .


Usage of variable-to-variable approach is recommended when you're planning some math operations on numerical variable and don't want to loose original value. In real life, you would practice on piece of paper before doing anything on original. When scripting in MMB, you don't have to worry about corrupting the original - you simply take another variable and perform changes on it ! Now that's a neat advantage, isn't it !

Talking about math, here are operations you can use in MMB script language:

Operator
Explanation
Examples
+
Addition of two or more numbers or numerical variables. Result of addition is assigned to destination numerical variable.
a=12+43
a=b+c
Weekend=Saturday+Sunday
DaysOfWeek=1+2+3+4+5+6+7
-

Subtraction of two or more numbers or numerical variables. Result of subtraction is assigned to destination numerical variable.

a=43-12
a=c-b
WorkDays=Week-Weekend
Tax=100-11-65
*
Multiplication of two or more numbers or numerical variables. Result is assigned to destination numerical variable.
a=43*12
a=c*b
Month=Week*4
Sales=24.99*buyers
/
Division of two or more numbers or numerical variables. Result of division is assigned to destination numerical variable. a=43/12
a=c/b
Month=Year/12
SpecWeight=quantity/30


Notice how many combinations are available here...

Math operations with two or more numbers:

a=432/21
SixPack=4+1+1

Math operations with two or more numerical variables:

a=b*c
Price=Quantity+ItemPrice- Taxes

Math operations with combinations of numbers and numerical variables:

a=b+42*NumberOfClicks
Price=2032*Quantity+83/ NumberOfMembers


All these are valid MMB script code lines ! You will find this combinations very useful in practice.

Once calculations on the right side are done, result is assigned to numerical variable on the left and you can use calculated value wherever you want, for example to show it as message:

Price=2032*Quantity+83/ NumberOfMembers
Message("Price of your order is: ","Price")

These two code lines will a) calculate order price, and b) display it using MMB's message box

Here's an example of multiple code lines with math operations:

days =365
years=100
millenium=1000
NumberOfDays=days*years* millenium
Message("Number of days in one millenium: ","NumberOfDays")

The result is 36500000 and it's displayed in MMB's message box.

Examples mentioned above already show you what numerical variables could be used for. And there are hundreds of other uses, especially when dealing with multimedia subject.


String Variables

For everything else except numbers used for math operations - you will use string variables.
Letters, words, sentences, paragraphs - are natural contents of string variables !

 


If we take this crystal glass and put words in it - we get string variable.

Just like that glass on image above, important part of string variable in MMB is label.

String variable label consits of name and $ sign.
Here are some examples of valid labels:

i$
n$
name$
1st_user$
Main_Title$
TextContent$
RegKey$
glass$


Never forget to attach $ after label name. That suffix tells MMB "This is string variable".

It is recommended to use meaningful & descriptive labels, not too short, not too long - just enough to describe it's content. If you'll use string variable to store user's answer to displayed question, label of that variable should be UserAnswer or AnswerNo1 .

Of course, important part of this story is - assigning of variable content.


In MMB script code lines, it is being done by:

a) writing label name:

sentence

b) adding suffix:

$

c) followed by equal sign:

=

d) and writing content (value) of variable at the end, behind label, $ suffix and equal sign.
Contents is enclosed in single quotes:

'My first sentence in MMB string variable !'


All together, MMB code line for assigning of sentece to string variable looks like this:

sentence$= 'My first sentence in MMB string variable !'


There ! With this line you instructed MMB: "Take a string variable sentence and put sentence My first sentence in MMB string variable ! in it."

Where will you give that instruction ? In MMB's Script Editor, of course.

Now, you're probably wondering: "What should I do with strings that contain ' character ?"
If you have a string:

What's this ?

...in MMB's string variable it will look like:

sentence$= 'What\'s this ?'

You use backslash \ to tell MMB "here ya go, I'm using single quote in my string variable..."

While backslash is obviously being used for this case in string variables, we must take care of the case when backslash is used for path to some directory (folder). String variable containing path to folder looks like this:

path$= 'c:\windows\\'

Example above contains double backslash, needed at the end of the string. Explanation is very simple - MMB uses one backslash before ' character so it can display that ' character.
If you have path in string, last backslash must be understood as "hey, we've got the end of a string, not ' character" , and the only way to do this is by using - double backslash.


Very nice. And just like in numerical variable case, the question is: can you only assign fixed words and never change 'em again ?

Nope !

It is important to have ability of assigning value of one variable to the value of another variable. Here's an example. If you have one bowl with words in it:

...what will you do to fill another bowl with words ?

You will copy words
from the first bowl

bowl1
=
bowl2


In MMB script code lines, it is being done by:

a) writing label of destination string variable (one that accepts value) with $ suffix:

bowl2$

b) followed by equal sign:

=

c) and writing label of source string variable (one that gives value) with $ suffix:

bowl1$


All together, MMB code line for assigning content of one string variable to another string variable looks like this::

bowl2$=bowl1$

If variable bowl1$ contains word Hello, code line above will assign the same value to bowl2$ .


Usage of variable-to-variable approach is recommended when you're planning some changes on string variable and don't want to loose original value.
When scripting in MMB, you don't have to worry about corrupting the original - just simply take another variable and perform changes on it.

To merge strings and put them into one string variable, use + operator.

For example, if definition of first string variable looks like this:

FirstPart$='To be - or not to be...'

And definition of second string variable is:

SecondPart$='the question is now.'

Merging of these strings into one looks like this:

CompleteSentence$=FirstPart$+SecondPart$

Merged strings are contents of CompleteSentence$:

To be - or not to be...the question is now

The same goal would be achieved by directly assigning text (but it's fixed then, can't be changed) to CompleteSentence$ :

CompleteSentence$= 'To be - or not to be...' + 'the question is now.'


Here are most common combinations of string variable assignments:

Definition
Example
Text to variable: fixed (non-changable) text is being assigned to string variable.
name$='Johnny Be Good'

Variable to variable: value of one string variable is being assigned to another string variable (for example, to retrieve value of MMB's EditBox object).

UserPassword$=EditBoxInput$
Variable+Text (or vice versa) to variable: using + for appending, this combination 1) merges contents of source string variable and source (fixed) text, 2) assigns result to destination string variable
a$=UserName$+' is hungry'

 

Contents (or results) of right-side strings are assigned to string variable on the left and you can use given values wherever you want, for example to show 'em as message:

a$=UserName$+' is hungry'
Message("Guess what: ","a$")

These two code lines will

a) assign strings to UserName$, and
b) display it using MMB's message box

Here's an example of multiple code lines with append operations:

Player1$='Doug'
Player2$=
'Steve'
MatchPlayers$= Player1$+' vs '+ Player2$
Message("And now, match: ","MatchPlayers$")

String displayed in MMB's message box would be:

And now, match: Doug vs Steve


String variables as object labels


Flexibility of MMB enables using content of string variable as a source for object labels, used for various script commands:

As an image above shows, object label "MyScript" is assigned to string variable (label$).
Once label is there, every script command that refers to some object label can use string variable instead of (fixed) label. So first you assign object label to a string variable:

ObjectLabel$= 'MyScript'

...and then execute script command that requires object label. For example, RunScript command uses one parameter - script label. Here we set string variable instead of fixed script label:

RunScript("ObjectLabel$")


This feature is very useful when objects have uniformed labels (Text1, Text2, Text3...) and performed commands should refer to variable labels - usually in for..next loops.

 

Examples mentioned above show you some uses of string variables. There are numerous purposes & combinations, so we leave you here to experiment and enjoy the power of strings !



Arrays


Remember those tool shelves and drawers in garage ? They have function of arrays - you put similar tools on the same shelve or drawer. Screws of different size go to different drawers.

Array is also shipment of some stocks in the truck:



On this truck you can see how entire shipment contains similar items (as the label on truck doors suggests). Every item has it's own number.

That's a reason why one would want to use arrays in program - they help programmer to hold similar data in one container. It's easier to load all similar items in one truck, than requesting many trucks with different names.

MMB uses arrays too. We call them 1-dimensional arrays, because items are stored in one "line" (dimension) - when your program puts or requests data from this kind of array, it uses only one coordinate (ordinal number of item) to go through array and perform operation on desired item.

Array in MMB is a group of variables under the same name, with addresses, so we can reach every item in array. In MMB script language, arrays are used just like variables.

Let's compare them...

Variable
Array
a$
a$[n]

name$

name$[n]
clicks
clicks[n]
user_address$
user_address$[n]
user_counter
user_counter[n]

 

On the left side are variables (string and numerical), and on the right are those same variables but in array form - extended form of variables, while we can store more items in arrays.

Let's take one array:

user_counter[n]

Here we have label of array ( user_counter ), two square brackets [ ] and item index ( n ). This is basic form of array. Array label can be specified just like in variable case. Square brackets enclose index number - either numerical variable or plain number that instructs MMB what item we want to retrieve from array.

If you've read section of manual about integer and string variables, you know they can be used almost anywhere in MMB. One of variable roles is to specify address of an item in array. These addresses are numbers, so we use numerical variables.

Here are some examples:

Arrays where item address is specified through ordinal numbers:

a[1]
twain$[72]
my_finger[534]
tool$[102]


Arrays where item address is specified through numerical variable:

a[i]
twain$[chapter_no]
my_finger[finger_number]
tool$[tool_drawer]

Arrays are frequently used in for...next loops, where variables are better solution for items addresses.

When you fill array, newest item gets highest address in array:

To specify item address you can even use math operations:

a[i + 1]
twain$[chapter_no - 4]
my_finger[finger_number * 8]
tool$[ tool_drawer / 2 ]

And how will you fill arrays ? Depends, what kind of array you're using:


Numerical arrays

Like numerical variables, MMB can use numerical arrays to store numbers. We use them for math operations. Both floating point and round numbers can be stored (83.21, 10, 294.1).

Let's fill some numerical arrays:

a[1] = c
my_finger[i] = 15
electricity[f + 1] = variable_volts

First example: array called " a[] " is filled with variable " c ", on item address 1.

Second example: array called " my_finger " is filled with number " 15 ", on item address received from variable " i " .

Third example: array called " electricity[] " is filled with variable " variable_volts ", on item address received from calculation: variable " f " + 1 .

Reading of items is similar:

c = a[1]
finger = my_finger[i]
power = electricity[f + 1]

First example: item on address " 1 ", from array " a[] ", is copied to numerical variable " c " .

Second example: item on address received from variable " i " , in array called " my_finger ", is copied to variable " finger " .

Third example: item on address calculated from variable " f " + 1, in array called "electricity[]", is copied to variable " power " .


String arrays

Like MMB string variable, this kind of array - string array - can store words, sentences, paragraphs of text and numbers. Basic difference in comparison with variables is possibility of storing many individual items.

String array looks like this:

a$[i]

It contains name of array ( a ), $ suffix that tells MMB we're using string array, square brackets [ ] and item address (numerical variable or ordinal number).

Filling string arrays looks like this:

a$[1] = c$
finger_names$[i] = name$
library$[f + 1] = book$

First example: array called " a$[] " is filled with content of variable " c$ ", on item address 1.

Second example: array called " finger_names$[] " is filled with content of variable " name$ ", on item address received from variable " i " .

Third example: array called " library$ " is filled with content of variable " book$ ", on item address received from calculation: numerical variable " f " + 1 .


Reading items from string arrays:

c$ = a$[1]
name$ = finger_names$[i]
book$ = library$[f + 1]

First example: item on address " 1 ", from array " a$[] ", is copied to string variable " c$ " .

Second example: item on address received from numerical variable " i " , in array called "finger_names$[]", is copied to string variable " name$ " .

Third example: item on address calculated from numerical variable " f " + 1, in array called " library$[] ", is copied to string variable " books$ " .


Examples of arrays in loop

As already mentioned, arrays are frequently used in loops. Here's one example:

for i=1 to 50
  a[i] = i
next i

This code will fill 50 items of numerical array called " a[] " with numbers from 1 to 50. Address of item is received from for...next loop.

Reading values after filling array can be done using loop too:

for i=1 to 50
  a = a [i]
  Message("Array item contents: ","a")
  Pause("200")
next i

Address of item is received from for...next loop. Every item is copied to numerical variable " a ", and then displayed in MMB's message box.


String arrays as object labels


Flexibility of MMB enables using content of string arrays as a source for object labels, used for various script commands:


As an image above shows, HotSpot object labels Spot1, Spot2 and Spot3 have been assigned to string array label$[], having items label$[1], label$[2] and label$[3] .

Once labels are there, every script command that refers to some object label can use string array item instead of (fixed) label. So first you assign object label to string array item:

label$[1]= 'Spot1'

...and then execute script command that requires object label. For example, RunScript command uses one parameter - script label. Here we set string array item instead of fixed object label:

RunScript("label$[1]")


This feature is very useful when objects have uniformed labels (Text1, Text2, Text3...) and performed commands should refer to variable labels - usually in for..next loops :

for i=1 to 10
  text$ = 'Hello no. '+CHAR(i)
  label$[i]='Text'+CHAR(i)
  LoadText("label$[i]","text$")
next i


For full understanding of MMB arrays it is highly recommended to read manual sections on variables and loop subjects.


Variable-related functions


Travelling ? It's a common practice to learn basics of foreign language to make your visit more comfortable. You can, of course, pay translator to do the job. Doing educational courses ? You'll give your best to translate knowledge to acceptable form, comprehendable by the course group.
Using computer ? By time you've read this paragraph, it performed millions of translations.

We're obviously talking about MMB, using some translations in script language too ! In sections above we have mentioned string and numerical variables with all their differences.

Using of variable-related functions makes variable differences disappear.

And to achieve that goal, we'll need a kind of translation. In MMB case, we'll perform translations between numerical and string variables.


CHAR(NumVariable)

converts numerical variable to a string variable

Specifying of numerical variable we want to convert to string is done by using source numerical variable label inside parenthesis:

CHAR(NumOfYears)

This would hardly make sense without specifying destination string variable, where converted numerical variable will be put. So we also need one string variable for the left side of code line:

Years$

Put together, code line for numerical -> string conversion looks like this:

Years$ =CHAR(NumOfYears)


Script line above will convert content of NumOfYears numerical variable to string and assign result to string variable Years$ .

This type of conversion (numbers to text) is used most often. Here's an example of displaying advanced string with data retrieved from numerical variable:

NumOfYears=36
UserAge$
=
CHAR(NumOfYears)+' years old'
Message("Our user is ","UserAge$")

Result of these code lines will be displayed in message box:

Our user is 36 years old

From this example is visible for what you'll use CHAR conversion - when you want to display numbers from numerical variable in string / text content.

 

VAL(StringVariable$)

converts string variable to numerical variable

Specifying of string variable we want to convert to number is done by using source string variable label inside parenthesis:

VAL(Year$)

This function requires specifying of destination numerical variable, where converted string variable will be put. So we also need one numerical variable for the left side of code line:

Year

Put together, code line for string -> number conversion looks like this:

Year =VAL(Year$)


Script line above will convert content of Year$ string variable to a number and assign result to numerical variable Year .

Here's an example of using VAL function to get year, perform math-related operation and display result in the message box:

Year$='2003'
Year=VAL(Year$)
Year=Year+10

Message("For ten years it will be: ","Year")

Result of this code lines will be displayed in message box:

For ten years it will be: 2013

From this example is visible for what you'll use VAL conversion - when you want to perform math operations on some number that was previously a string (text).

 

INT(NumVariable)

rounds floating point number to lower integer value

Specifying of numerical variable we want to round value for is done using source numerical variable label inside parenthesis:

INT(CarTax)

This function requires specifying destination numerical variable, where integer number will be put. So we also need one numerical variable for the left side of code line:

RoundCarTax

Put together, code line for floating point -> integer conversion looks like this:

RoundCarTax =INT(CarTax)


Script line above will round floating point content of CarTax numerical variable to integer and assign result to numerical variable RoundCarTax .

Here's an example of using INT function in practice:

CarTax=14.32
RoundCarTax=INT(CarTax)
Message
("Rounded tax for car is: ","RoundCarTax")

Result of this code lines will be displayed in the message box:

Rounded tax for car is: 14

Notice how variable is rounded to lower value. This is being done in all cases:

CarTax=14.99
RoundCarTax=INT(CarTax)
Message
("Rounded tax for car is: ","RoundCarTax")

Result of INT function in this case will not be rounded to a higher value:

Rounded tax for car is: 14

INT function is in MMB mostly used for percentage counting (progress bars, gauges).

 

ABS(NumVariable)

rounds floating point number to a lower integer & absolute value

What "absolute" means ? Number can't be negative. If numerical variable content is lower than zero, negative prefix is removed and number becomes positive (above zero).

Specifying numerical variable we want to round & absolute value for is performed using source numerical variable label inside parenthesis:

ABS(CupsOfCoffee)

This function requires specifying destination numerical variable, where round & absolute number will be put. So we also need one numerical variable for the left side of the code line:

CoffeeCups

Put together, code line for real number -> absolute number conversion looks like this:

CoffeeCups =ABS(CupsOfCoffee)


Script line above will (optionally) round floating point content of CupsOfCoffee numerical variable to integer, check if number is negative so it can be converted to positive value and assign result to the numerical variable CoffeeCups .

Here's an example of using ABS function in practice:

CupsOfCoffee=-5.5
CoffeeCups=ABS(CupsOfCoffee)
Message
("Number of coffee cups to order: ","CoffeeCups")

Result of these code lines will be displayed in message box:

Number of coffee cups to order: 5

ABS function is mostly used for prevention of negative numerical values.

 

RND(NumVariable)

generates random number in the range specified using numerical variable

This is specific variable-related function. It does not convert input variable, but uses it as a range limiter when generating random numbers.

Specifying numerical variable that is upper limit for random number range is done by using source numerical variable label or fixed number inside parenthesis:

RND(UpLimit)

...or...

RND(48)

This function requires specifying destination numerical variable, where generated random number will be put. So we also need one numerical variable for the left side of a code line:

LottoNumber


Put together, code line for random number generator looks like this:

LottoNumber =RND(UpLimit)

...or...

LottoNumber =RND(48)


Script lines above will use content of numerical variable (UpLimit) or fixed number (48) as upper limit of generated random number and assign result to numerical variable LottoNumber . Generated number will be within range 0 - upper limit .

Here's an example of using RND function in practice:

LottoNumber=RND(48)
Message
("Today's lucky number is: ","LottoNumber")

Result of this code lines will display message box text...

Today's lucky number is:

...and append random number (RND function result) to it.

Calling RND function repeatedly will generate numbers that differ from previous ones. Of course, having limited range, numbers will eventually repeat.

 

 

MMB Scripting Unleashed by Bokzy, 2003 :: All rights reserved :: http://www.bokzy.com